<?php
/**
 * Created by PhpStorm.
 * User: serenehaly
 * Date: 2018/2/2
 * Time: 15:24
 */

namespace app\api\model;


class Product extends BaseModel
{
    public $hidden = ['delete_time', 'category_id', 'from', 'create_time', 'update_time', 'pivot'];

    public function getMainImgUrlAttr($url, $data)
    {
        return $this->prefixImgUrl($url, $data);
    }

    public function imgs()
    {
        $result = $this->hasMany('ProductImage', 'product_id', 'id');
        return $result;
    }

    public function property()
    {
        $result = $this->hasMany('ProductProperty', 'product_id', 'id');
        return $result;
    }

    public static function getRecend($count)
    {
        $product = self::limit($count)->order('create_time', 'desc')->select();
        return $product;
    }

    public static function getAllByCategoryID($id)
    {
        $product = self::where('category_id', '=', $id)->select();
        return $product;
    }

    public static function getProductByID($id)
    {
        $product = self::with([
        'imgs' => function($query) {
            $query->with(['imgUrl'])
            ->order('order', 'asc');
        }
    ])
            ->with(['property'])
            ->where('id', '=', $id)
            ->find();
        return $product;
    }


}